/*
 * Android Runner is a multiplayer GPS game fully written by Xurxo Mendez Perez
 * 
 * Copyright (C) 2009 Xurxo Mendez Perez
 *   
 * This file is part of Android Runner.
 * 
 * Android Runner is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Android Runner is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Android Runner.  If not, see <http://www.gnu.org/licenses/>.
 */

package es.sonxurxo.gpsgame.client.cv.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ProgressBar;
import android.widget.TextView;
import es.sonxurxo.gpsgame.client.R;
import es.sonxurxo.gpsgame.client.cv.to.Options;

/**
 * 
 * @author "Xurxo Mendez Perez"
 *
 */
public class OptionsActivity extends Activity {

	private Button saveButton;
	private Options options;
	private ProgressBar progressHorizontal;
	private TextView interval;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        options = (Options)getIntent().getExtras().getSerializable("options");
        
        setContentView(R.layout.options_page);
        setProgressBarVisibility(true);
        interval = 
        	((TextView)findViewById(R.id.interval));
        interval.setText(String.valueOf(options.getUpdatesInterval()));
        progressHorizontal = 
        	(ProgressBar) findViewById(R.id.progress_horizontal);

        progressHorizontal.setProgress(options.getUpdatesInterval());
        Button button = (Button) findViewById(R.id.progress_rest);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-5);
                interval.setText(String.valueOf(progressHorizontal.getProgress()));
            }
        });
        
        Button button2 = (Button) findViewById(R.id.progress_add);
        button2.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(5);
                interval.setText(String.valueOf(progressHorizontal.getProgress()));
            }
        });
        
        ((CheckBox)findViewById(R.id.op_other_gets)).setChecked(options.isNotifyGets());
        ((CheckBox)findViewById(R.id.op_messages)).setChecked(options.isNotifyMessages());
        ((CheckBox)findViewById(R.id.op_other_joins)).setChecked(options.isNotifyJoins());
        ((CheckBox)findViewById(R.id.op_other_leaves)).setChecked(options.isNotifyLeaves());
        ((CheckBox)findViewById(R.id.op_followMe)).setChecked(options.isFollowMe());
        ((CheckBox)findViewById(R.id.op_show_ranking)).setChecked(options.isShowRankingOnGet());
        saveButton = (Button)findViewById(R.id.op_save);
        saveButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {
				doSave();
			}
        	
        });
	}
	
	private void doSave() {
		options.setNotifyGets(((CheckBox)findViewById(R.id.op_other_gets)).isChecked());
		options.setNotifyJoins(((CheckBox)findViewById(R.id.op_other_joins)).isChecked());
		options.setNotifyLeaves(((CheckBox)findViewById(R.id.op_other_leaves)).isChecked());
		options.setFollowMe(((CheckBox)findViewById(R.id.op_followMe)).isChecked());
		options.setNotifyMessages(((CheckBox)findViewById(R.id.op_messages)).isChecked());
		options.setShowRankingOnGet(((CheckBox)findViewById(R.id.op_show_ranking)).isChecked());
		options.setUpdatesInterval(progressHorizontal.getProgress());
		Intent mIntent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putSerializable("options", options);
		mIntent.putExtras(bundle);
        setResult(RESULT_OK, mIntent);
		finish();
	}
}
